package com.jugheadzhou.practice.leecode;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 剑指 Offer II 041. 滑动窗口的平均值
 */
public class MovingAverageTest {

    private int size;
    private double sum;
    private Queue<Integer> dataQue;

    /** Initialize your data structure here. */
    public MovingAverageTest(int size) {
        this.size = size;
        dataQue = new LinkedList<Integer>();
        sum = 0;
    }

    public double next(int val) {
        if (dataQue.size() == size){
            sum -= dataQue.poll();
        }
        dataQue.offer(val);
        sum += val;
        return sum / dataQue.size();
    }


    public static void main(String[] args) {
        MovingAverageTest averageTest = new MovingAverageTest(3);
        System.out.println("averageTest.next(1) = " + averageTest.next(1));
        System.out.println("averageTest.next(10) = " + averageTest.next(10));
        System.out.println("averageTest.next(3) = " + averageTest.next(3));
        System.out.println("averageTest.next(5) = " + averageTest.next(5));
    }
}
